<?php

class CommodityRequire_Model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @param $row
     * @return CommodityRequire
     */
    public static function getCommodityRequireByRow($row)
    {
        $thatCommodityRequire = new CommodityRequire;
        $thatCommodityRequire->setId($row->id);
        $thatCommodityRequire->setTitle(strip_tags($row->title));
        $thatCommodityRequire->setContent(strip_tags($row->content));
        $thatCommodityRequire->setMinPrice($row->min_price);
        $thatCommodityRequire->setMaxPrice($row->max_price);
        $thatCommodityRequire->setContactType($row->contact_type);
        $thatCommodityRequire->setContactInfo($row->contact_info);
        $thatCommodityRequire->setNum($row->num);
        $thatCommodityRequire->setReleaseType($row->release_type);
        $thatCommodityRequire->setReleaseTime($row->release_time);
        $thatCommodityRequire->setUserId($row->user_id);
        $thatCommodityRequire->setCommodityCatId($row->commodity_cat_id);
        $thatCommodityRequire->setCommodityTagsIdJson($row->commodity_tags_id_json);
        $thatCommodityRequire->setSchoolunicode($row->schoolunicode);
        return $thatCommodityRequire;
    }

    /**
     * @param CommodityRequire $thatCommodityRequire
     * @return array
     */
    public static function getModelByCommodityRequire($thatCommodityRequire)
    {
        return array(
            'title'                  => strip_tags($thatCommodityRequire->getTitle()),
            'content'                => strip_tags($thatCommodityRequire->getContent()),
            'min_Price'              => $thatCommodityRequire->getMinPrice(),
            'max_Price'              => $thatCommodityRequire->getMaxPrice(),
            'num'                    => $thatCommodityRequire->getNum(),
            'contact_type'           => $thatCommodityRequire->getContactType(),
            'contact_info'           => $thatCommodityRequire->getContactInfo(),
            'release_type'           => $thatCommodityRequire->getReleaseType(),
            'release_time'           => $thatCommodityRequire->getReleaseTime(),
            'user_id'                => $thatCommodityRequire->getUserId(),
            'commodity_cat_id'       => $thatCommodityRequire->getCommodityCatId(),
            'commodity_tags_id_json' => $thatCommodityRequire->getCommodityTagsIdJson(),
            'schoolunicode'          => $thatCommodityRequire->getSchoolunicode(),
        );
    }

    /**
     * @param int $id
     * @return CommodityRequire
     */
    public static function getCommodityRequireById($id)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('commodity_require.*');
        $CI->db->from('commodity_require');
        $CI->db->where('commodity_require.id', $id);

        $query = $CI->db->get();
        $row = $query->row();
        if ($row)
            return self::getCommodityRequireByRow($row);
        else
            return null;
    }

    /**
     * @param string $title
     * @return CommodityRequire
     */
    public static function getCommodityRequireByTitle($title)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('commodity_require.*');
        $CI->db->from('commodity_require');
        $CI->db->where('commodity_require.title', $title);

        $query = $CI->db->get();
        $row = $query->row();
        if ($row)
            return self::getCommodityRequireByRow($row);
        else
            return null;
    }

    /**
     * @param bool|int $limit
     * @param bool|int $offset
     * @param string $orderBy
     * @param string $orderType
     * @return array
     */
    public static function getCommodityRequireArray($limit = false, $offset = false, $orderBy = 'id', $orderType = 'desc')
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('commodity_require.*');
        $CI->db->from('commodity_require');
        $currentSchool = YimeTools::getCurrentSchool();
        if ($currentSchool['schoolcode']) {
            $CI->db->where('commodity_require.schoolunicode', $currentSchool['schoolcode']);
        }
        $CI->db->order_by($orderBy, $orderType);

        if ($limit !== false && is_numeric($limit)) {
            if ($offset === false || !is_numeric($offset))
                $offset = 0;
            $CI->db->limit($limit, $offset);
        }

        $query = $CI->db->get();
        $result = $query->result();

        $array = array();
        foreach ($result as $row) {
            $array[] = self::getCommodityRequireByRow($row);
        }
        return $array;
    }

    /**
     * @param int $userId
     * @param bool|int $limit
     * @param bool|int $offset
     * @param string $orderBy
     * @param string $orderType
     * @return array
     */
    public static function getCommodityRequireArrayByUserId($userId = 0, $limit = false, $offset = false, $orderBy = 'id', $orderType = 'desc')
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('commodity_require.*');
        $CI->db->from('commodity_require');
        $CI->db->where('user_id', $userId);
        $CI->db->order_by($orderBy, $orderType);

        if ($limit !== false && is_numeric($limit)) {
            if ($offset === false || !is_numeric($offset))
                $offset = 0;
            $CI->db->limit($limit, $offset);
        }

        $query = $CI->db->get();
        $result = $query->result();

        $array = array();
        foreach ($result as $row) {
            $array[] = self::getCommodityRequireByRow($row);
        }
        return $array;
    }

    /**
     * @param int $commodityCatId
     * @param int|bool $limit
     * @param int|bool $offset
     * @param string $orderBy
     * @param string $orderType
     * @return array
     */
    public static function getCommodityRequireArrayByCommodityCatId($commodityCatId, $limit = false, $offset = false, $orderBy = 'id', $orderType = 'desc')
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('commodity_require.*');
        $CI->db->from('commodity_require');
        $CI->db->where('commodity_cat_id', $commodityCatId);
        $CI->db->order_by($orderBy, $orderType);

        if ($limit !== false && is_numeric($limit)) {
            if ($offset === false || !is_numeric($offset))
                $offset = 0;
            $CI->db->limit($limit, $offset);
        }

        $query = $CI->db->get();
        $result = $query->result();

        $array = array();
        foreach ($result as $row) {
            $array[] = self::getCommodityRequireByRow($row);
        }
        return $array;
    }

    public static function getCommodityRequireArrayByReleaseType($releaseType, $limit = false, $offset = false)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('commodity_require.*');
        $CI->db->from('commodity_require');
        $CI->db->where('release_type', $releaseType);
        $currentSchool = YimeTools::getCurrentSchool();
        if ($currentSchool['schoolcode']) {
            $CI->db->where('commodity_require.schoolunicode', $currentSchool['schoolcode']);
        }
        $CI->db->order_by('id', 'desc');

        if ($limit !== false && is_numeric($limit)) {
            if ($offset === false || !is_numeric($offset))
                $offset = 0;
            $CI->db->limit($limit, $offset);
        }

        $query = $CI->db->get();
        $result = $query->result();

        $array = array();
        foreach ($result as $row) {
            $array[] = self::getCommodityRequireByRow($row);
        }
        return $array;
    }

    /**
     * @return int
     */
    public static function countCommodityRequire()
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('commodity_require.id');
        $CI->db->from('commodity_require');

        return $CI->db->count_all_results();
    }

    /**
     * @param $userId
     * @return int
     */
    public static function countCommodityRequireByUserId($userId)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('commodity_require.*');
        $CI->db->from('commodity_require');
        $CI->db->where('commodity_require.user_id', $userId);

        return $CI->db->count_all_results();
    }

    /**
     * @param $releaseType
     * @return int
     */
    public static function countCommodityRequireByReleaseType($releaseType)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->select('commodity_require.*');
        $CI->db->from('commodity_require');
        $CI->db->where('release_type', $releaseType);

        return $CI->db->count_all_results();
    }

    /**
     * @param CommodityRequire $thatCommodityRequire
     * @throws DatabaseException
     * @return CommodityRequire
     */
    public static function add($thatCommodityRequire)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $result = $CI->db->insert('commodity_require', self::getModelByCommodityRequire($thatCommodityRequire));
        if ($result)
            return self::getCommodityRequireById($CI->db->insert_id());
        else
            throw new DatabaseException('插入数据失败');
    }

    /**
     * @param CommodityRequire $thatCommodityRequire
     * @throws DatabaseException
     * @return CommodityRequire
     */
    public static function update($thatCommodityRequire)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->where('id', $thatCommodityRequire->getID());
        $result = $CI->db->update('commodity_require', self::getModelByCommodityRequire($thatCommodityRequire));
        if ($result)
            return self::getCommodityRequireById($thatCommodityRequire->getID());
        else
            throw new DatabaseException('更新数据失败');
    }

    /**
     * @param CommodityRequire $thatCommodityRequire
     * @throws DatabaseException
     * @return bool
     */
    public static function delete($thatCommodityRequire)
    {
        /** @var CI $CI */
        $CI =& get_instance();

        $CI->db->where('id', $thatCommodityRequire->getID());
        $result = $CI->db->delete('commodity_require');
        if ($result)
            return true;
        else
            throw new DatabaseException('删除数据失败');
    }

}
